package com.example.indenture.user.dao;


import com.example.indenture.common.vo.Search;
import com.example.indenture.user.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.List;


@Repository
@Mapper
public interface UserDao {

    @Insert("insert into user(user_name, create_date, update_time,email,password,phone,sex,state,status,url) " +
            "values(#{userName}, #{createDate}, #{updateTime},#{email}, #{password}, #{phone}, #{sex}, #{state}, #{status}, #{url})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void insertUser(User user);

    @Select("select * from user where user_name = #{userName} And state = 1 limit 1")
    User getUserByUserName(String userName);

    @Select("select * from user where email = #{email} And state = 1 limit 1")
    User getUserByEmail(String email);

    @Select("select * from user where phone = #{phone} And state = 1 limit 1")
    User getUserByPhone(String phone);


    @Update("update user set state=0 where id = #{id}")
    void deleteUserById(int id);

    //更新

    @Update("<script>" +
            "update user" +
            "<set>" +
            " <if test='password != null'> password=#{password},</if>" +
            "user_name = #{userName}," +
            "update_time = #{updateTime}," +
            "email=#{email}," +
            "phone=#{phone}," +
            "sex=#{sex}" +
            "</set>" +
            "where id = #{id}" +
            "</script>")
    void updateUser(User user);

    @Select("select * from user where user_name = #{userName} AND password = #{password} AND state = 1")
    User getUserByUserNameAndPassword(String userName, String password);

    @Select("<script>"
            + "select * from user "
            + "<where> " +
            "state=1"
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (user_name like '%${keyword}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='sort != \"\" and sort != null'>"
            + " Order by ${sort} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " Order by id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<User> getUsersBySearch(Search search);

    @Update("update user set url=#{url} where id = #{id}")
    void updateUserUrl(int id, String url);

    @Select("select * from user where id = #{id}")
    User getUserById(int id);

    //根据用户name查id
    @Select("select id from user where status = '2' and user_name = #{name} ")
    Integer getIdByName(String name);

    @Select("select * from user where user_name=#{username}")
    User getPasswordByName(String username);

    @Select(("select * from user where id=#{id}"))
    User getUserById2(Integer id);
}
